package org.budo.activiti.controller;

import java.io.InputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.budo.support.lang.util.IoUtil;
import org.budo.support.servlet.util.ResponseUtil;
import org.budo.support.spring.io.util.ResourceUtil;

import lombok.extern.slf4j.Slf4j;

/**
 * @author lmw
 */
@Slf4j
public abstract class AbstractActivitiController {
    protected void handleEditorAppRequest(HttpServletRequest request, HttpServletResponse response) {
        ResponseUtil.setCacheHeader();

        String requestURI = request.getRequestURI();
        log.debug("#58 requestURI=" + requestURI);

        int index = requestURI.indexOf("/editor-app/");
        String sub = requestURI.substring(index);

        String path = "static" + (sub.startsWith("/") ? "" : "/") + sub;

        InputStream inputStream = ResourceUtil.classPathResourceInputStream(path);
        IoUtil.write(inputStream, ResponseUtil.getOutputStream(response));
    }

    protected void handleStaticRequest(HttpServletRequest request, HttpServletResponse response) {
        ResponseUtil.setCacheHeader();

        String requestURI = request.getRequestURI();
        log.debug("#44 requestURI=" + requestURI);

        int index = requestURI.indexOf("/static/");
        String path = requestURI.substring(index);

        InputStream inputStream = ResourceUtil.classPathResourceInputStream(path);
        IoUtil.write(inputStream, ResponseUtil.getOutputStream(response));
    }
}